class Solution(object):
    def canCompleteCircuit(self, gas, cost):
        """
        :type gas: List[int]
        :type cost: List[int]
        :rtype: int
        """
        length = len(gas)
        left_index = 0
        right_index = 0
        value = gas[left_index] - cost[left_index]
        while True:
            if value < 0:
                left_index = right_index
                while value < 0:
                    left_index += 1
                    if left_index >= length:
                        return -1
                    value = gas[left_index] - cost[left_index]
                right_index = left_index
            else:
                right_index += 1
                if right_index - left_index == length:
                    return left_index
                else:
                    value += gas[right_index % length] - cost[right_index % length]


data = Solution()
gas = [2,3,4]
cost = [3,4,3]
print(data.canCompleteCircuit(gas, cost))
